#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <time.h>
#include <math.h>

float Dist2Point(float x1, float y1, float x2, float y2)
{
	float result;
	result = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
	return result;
}

int isTwoCircle(float x1, float y1, float r1, float x2, float y2, float r2)
{
	if (Dist2Point(x1, y1, x2, y2) < r1 + r2)
		return 1;
	return 0;
}

int randR(int min,int max)
{
	int r = rand() % (max - min + 1) + min;
	return r;
}

//黄色绘制
void Draw1(float x, float y, float r)
{
	setlinecolor(BLACK);
	setfillcolor(RGB(255, 255, 0));
	fillcircle(x, y, r);
}

void Draw2(float x, float y, float r)
{
	float h = rand() % 360;
	COLORREF color = HSVtoRGB(h, 0.6, 0.8);
	setlinecolor(BLACK);
	setfillcolor(color);
	fillcircle(x, y, r);
}

void Draw3(float x, float y, float r)
{
	while (r > 0)
	{
		float h = rand() % 360;
		COLORREF color = HSVtoRGB(h, 0.6, 0.8);
		setlinecolor(BLACK);
		setfillcolor(color);
		fillcircle(x, y, r);
		r -= 5;
	}
}

void Draw4(float x, float y, float r)
{
	while (r > 0)
	{
		float h = rand() % 360;
		COLORREF color = HSVtoRGB(h, 0.9, 0.8);
		setlinecolor(color);
		circle(x, y, r);
		r -= 5;
	}
}

int main()
{
	int width = 600;
	int height = 600;
	initgraph(width, height);
	setbkcolor(WHITE);
	cleardevice();
	srand(time(NULL));

	int xArray[1000];
	int yArray[1000];
	int rArray[1000];
	int rmin = 8;
	int rmax = 50;
	int circleNum = 0;
	float x, y, r;
	int isNewCircle = 0;
	int i,j;
	int drawmode = 3;

	while (circleNum < 1000)
	{
		isNewCircle = 0;

		while (isNewCircle == 0)
		{
			if (_kbhit())
			{
				char input = _getch();
				if (input == ' ')
				{
					circleNum = 0;
					cleardevice();
					drawmode += 1;
					if (drawmode > 4)
						drawmode = 1;
				}
			}
			x = rand() % width;
			y = rand() % height;
			r = rand() % (rmax - rmin + 1) + rmin;

			for (i = 0; i < circleNum; i++)
				if (isTwoCircle(xArray[i],yArray[i],rArray[i],x,y,r))
					break;

			if (i == circleNum)
				isNewCircle = 1;
		}

		isNewCircle = 0;

		while (isNewCircle == 0 && r < rmax)
		{
			r++;
			for (j = 0; j < circleNum; j++)
			{
				if (isTwoCircle(xArray[j], yArray[j], rArray[j], x, y, r))
				{
					isNewCircle = 1;
					break;
				}
			}
		}
		
		xArray[circleNum] = x;
		yArray[circleNum] = y;
		rArray[circleNum] = r;
		circleNum++;

		if (drawmode == 1)
			Draw1(x,y,r);
		if (drawmode == 2)
			Draw2(x, y, r);
		if (drawmode == 3)
			Draw3(x, y, r);
		if (drawmode == 4)
			Draw4(x, y, r);

		Sleep(10);
	}

	_getch();
	closegraph();
	return 0;
}